package net.cmbt.okframe.okbanner;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import net.cmbt.okframe.R;

import java.util.List;

/**
 * Created by LG on 2019/12/9.
 */

public class BannerAdapter extends PagerAdapter {
    private List<String> mList;
    OKBannerNormal okBannerNormal;

    public BannerAdapter(List<String> mList, OKBannerNormal okBannerNormal) {
        this.mList = mList;
        this.okBannerNormal = okBannerNormal;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View banner_item = View.inflate(container.getContext(), R.layout.banner_item, null);
        ImageView banner_img = (ImageView) banner_item.findViewById(R.id.banner_img);
        Glide.with(container.getContext()).load(mList.get(position % mList.size())).into(banner_img);
        container.addView(banner_item);
        banner_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                okBannerNormal.bannerListener.onClick(position % mList.size(), v);
            }
        });
        return banner_item;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View imageView = (View) object;
        container.removeView(imageView);
    }
}
